//
//  ChatCell.m
//  AILove
//
//  Created by Ace Portgas on 2023/12/28.
//

#import "ChatCell.h"
#import "ChatFrame.h"
#import "ChatModel.h"
@interface ChatCell()
@property (strong,nonatomic) UIImageView * avatar;
@property (strong,nonatomic) UILabel * content;
@property (strong,nonatomic) UIView * contentBackView;
@property (strong,nonatomic) LOTAnimationView * lottieView;
@end
@implementation ChatCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self initView];
        self.backgroundColor = [UIColor clearColor];
        self.contentView.backgroundColor = [UIColor clearColor];
    }
    return self;
}

-(void)initView{
    _avatar = [[UIImageView alloc]init];
    _avatar.layer.cornerRadius = 19;
    _avatar.layer.masksToBounds = YES;
    _avatar.contentMode = UIViewContentModeScaleAspectFill;
    [self.contentView addSubview:_avatar];
    
    _contentBackView = [UIView new];
    [self.contentView addSubview:_contentBackView];
    
    _content = [UILabel new];
    _content.font = [UIFont fontWithName:@"PingFangSC-Medium" size:14];
    _content.textColor = [UIColor colorWithHexString:@"ffffff"];
    _content.textAlignment = NSTextAlignmentLeft;
    _content.numberOfLines = 0;
    [_contentBackView addSubview:_content];
    
    _voice = [UIButton buttonWithType:UIButtonTypeCustom];
    [_voice setBackgroundImage:SQGetImage(@"按钮背景") forState:UIControlStateNormal];
    [_voice setImage:SQGetImage(@"语音") forState:UIControlStateNormal];
    [_voice setImage:SQGetImage(@"聊天声音暂停") forState:UIControlStateSelected];
    [_voice addTarget:self action:@selector(playVoice) forControlEvents:UIControlEventTouchUpInside];
    _voice.imageView.animationImages = @[
        [UIImage imageNamed:@"语音 2"],
        [UIImage imageNamed:@"语音 2"],
        [UIImage imageNamed:@"语音 2"],
        [UIImage imageNamed:@"语音 2"],
        [UIImage imageNamed:@"语音 2"],
        [UIImage imageNamed:@"语音 2"],
        [UIImage imageNamed:@"语音 2"],
        [UIImage imageNamed:@"语音 2"],
        [UIImage imageNamed:@"语音 2"],
        [UIImage imageNamed:@"语音 2"],
        [UIImage imageNamed:@"语音 1"],
        [UIImage imageNamed:@"语音 1"],
        [UIImage imageNamed:@"语音 1"],
        [UIImage imageNamed:@"语音 1"],
        [UIImage imageNamed:@"语音 1"],
        [UIImage imageNamed:@"语音 1"],
        [UIImage imageNamed:@"语音 1"],
        [UIImage imageNamed:@"语音 1"],
        [UIImage imageNamed:@"语音 1"],
        [UIImage imageNamed:@"语音 1"],
        [UIImage imageNamed:@"语音"],
        [UIImage imageNamed:@"语音"],
        [UIImage imageNamed:@"语音"],
        [UIImage imageNamed:@"语音"],
        [UIImage imageNamed:@"语音"],
        [UIImage imageNamed:@"语音"],
        [UIImage imageNamed:@"语音"],
        [UIImage imageNamed:@"语音"],
        [UIImage imageNamed:@"语音"],
        [UIImage imageNamed:@"语音"]
    ];
    [self.contentView addSubview:_voice];
    
    _lottieView = [LOTAnimationView animationNamed:@"chat_recieving.json"];
    _lottieView.loopAnimation = YES;
    _lottieView.hidden = NO;
    _lottieView.layer.masksToBounds = YES;
    [_contentBackView addSubview:_lottieView];
}

-(void)playVoice{
    if (self.voice.imageView.isAnimating) {
        // 动画开始播放
        [self.voice.imageView stopAnimating];
    } else {
        // 动画停止
        [self.voice.imageView startAnimating];
    }
    if (self.delegate && [self.delegate respondsToSelector:@selector(playVoiceWithUrl:andCell:)]) {
        [self.delegate playVoiceWithUrl:_frameModel.model.voiceUrl andCell:self];
    }
}

-(void)setFrameModel:(ChatFrame *)frameModel{
    _frameModel = frameModel;
    _avatar.frame = frameModel.avatarFrame;
    _voice.frame = frameModel.voiceFrame;
    _content.frame = frameModel.contentFrame;
    _contentBackView.frame = frameModel.contentBackFrame;
    _lottieView.frame = frameModel.lottieFrame;
    if (frameModel.model.sendType == 1) {
        if (_frameModel.model.content.length == 0) {
            _lottieView.hidden = NO;
            [_lottieView play];
        }else{
            [_lottieView stop];
            _lottieView.hidden = YES;
        }
        _contentBackView.backgroundColor = [UIColor colorWithHexString:@"#000000" alpha:0.8];
        // 添加圆角
        UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:self.contentBackView.bounds
                                                       byRoundingCorners:UIRectCornerTopRight | UIRectCornerBottomRight | UIRectCornerTopLeft
                                                             cornerRadii:CGSizeMake(8, 8)];
        CAShapeLayer *maskLayer = [CAShapeLayer layer];
        maskLayer.frame = self.contentBackView.bounds;
        maskLayer.path = maskPath.CGPath;
        self.contentBackView.layer.mask = maskLayer;
        
        // 添加圆角
        UIBezierPath * voiceMaskPath = [UIBezierPath bezierPathWithRoundedRect:self.voice.bounds
                                                       byRoundingCorners:UIRectCornerTopRight | UIRectCornerBottomRight | UIRectCornerTopLeft
                                                             cornerRadii:CGSizeMake(14, 14)];
        CAShapeLayer * voiceMaskLayer = [CAShapeLayer layer];
        voiceMaskLayer.frame = self.contentBackView.bounds;
        voiceMaskLayer.path = voiceMaskPath.CGPath;
        self.voice.layer.mask = voiceMaskLayer;
    }else{
        [_lottieView stop];
        _lottieView.hidden = YES;
        _contentBackView.backgroundColor = [UIColor colorWithHexString:@"#E62DFD" alpha:1];
        // 添加圆角
        UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:self.contentBackView.bounds
                                                       byRoundingCorners:UIRectCornerTopRight | UIRectCornerBottomLeft | UIRectCornerTopLeft
                                                             cornerRadii:CGSizeMake(8, 8)];
        CAShapeLayer *maskLayer = [CAShapeLayer layer];
        maskLayer.frame = self.contentBackView.bounds;
        maskLayer.path = maskPath.CGPath;
        self.contentBackView.layer.mask = maskLayer;
    }
    _content.text = frameModel.model.content;
}

-(void)setCover:(NSString *)cover{
    _cover = cover;
    [self.avatar sd_setImageWithURL:SQURLImage(cover)];
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
